import paypalrestsdk

paypalrestsdk.configure({
  "mode": "sandbox", # sandbox or live
  "client_id": "AV4Qd-byRrKnWSWa4PsuHNAI45rvZWZsCMe3FpOhvWBnd5ifS_FvaQS-wLWdTLOCCZksQFw1eR0kbm4h",
  "client_secret": "ECRo9sXE2D2F7JMe8JOKKHIOpYdDNwrQCIi4IIoxBZRpPQdk_PhqS1jokMtJ34QZlt_KlrmWTJJwqcO3" })



def payment(request):

    paypalrestsdk.configure({
  "mode": "sandbox", # sandbox代表沙盒
  "client_id": "你的client_id,
  "client_secret": "你的client_secret" })

    payment = paypalrestsdk.Payment({
        "intent": "sale",
        "payer": {
            "payment_method": "paypal"},
        "redirect_urls": {
            "return_url": "http://localhost:8000/palpay/pay/",#支付成功跳转页面
            "cancel_url": "http://localhost:3000/paypal/cancel/"},#取消支付页面
        "transactions": [{
            "amount": {
                "total": "5.00",
                "currency": "USD"},
            "description": "这是一个订单测试"}]})

    if payment.create():
        print("Payment created successfully")
        for link in payment.links:
            if link.rel == "approval_url":
                approval_url = str(link.href)
                print("Redirect for approval: %s" % (approval_url))
                return redirect(approval_url)
    else:
        print(payment.error)
        return HttpResponse("支付失败")


def payment_execute(request):

    paymentid = request.Get.get("paymentId") #订单id
    payerid = request.Get.get("PayerID")  #支付者id

    payment = paypalrestsdk.Payment.find(paymentid)


    if payment.execute({"payer_id": payerid}):
        print("Payment execute successfully")
        return HttpResponse("支付成功")
    else:
        print(payment.error) # Error Hash
        return HttpResponse("支付失败")





payment = paypalrestsdk.Payment({
    "intent": "sale",
    "payer": {
        "payment_method": "paypal"},
    "redirect_urls": {
        "return_url": "http://localhost:8000/palpay/pay/",
        "cancel_url": "http://localhost:3000/paypal/cancel/"},
    "transactions": [{
        "amount": {
            "total": "5.00",
            "currency": "USD"},
        "description": "这是一个订单测试"}]})

if payment.create():
  print("Payment created successfully")
else:
  print(payment.error)

paymentid = payment.id

print(paymentid)


for link in payment.links:
    if link.rel == "approval_url":
        approval_url = str(link.href)
        print("Redirect for approval: %s" % (approval_url))


#支付   ETYYRCDN8C3XJ

payment = paypalrestsdk.Payment.find("PAYID-L3SYORA3C031930S1733650J")


if payment.execute({"payer_id": "ETYYRCDN8C3XJ"}):
  print("Payment execute successfully")
else:
  print(payment.error) # Error Hash


#明细

payment = paypalrestsdk.Payment.find("PAYID-L3SYORA3C031930S1733650J")
print(payment)



#退款
from paypalrestsdk import Sale

sale = Sale.find("43W86384NS455771X")

# Make Refund API call
# Set amount only if the refund is partial
refund = sale.refund({
    "amount": {
        "total": "5.00",
        "currency": "USD"}})

# Check refund status
if refund.success():
    print("Refund[%s] Success" % (refund.id))
else:
    print("Unable to Refund")
    print(refund.error)